//
//  UserDataModel.m
//  AddressBook
//
//  Created by mac on 2017/8/30.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserDataModel.h"
#import "NSObject+XQArchiverData.h"

#define UD [NSUserDefaults standardUserDefaults]

static NSString * UD_UserData = @"UserData";

@implementation UserDataModel

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        [self decoder:aDecoder];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [self encoder:aCoder];
}

/*
{
    code = 10000;
    data =     (
                {
                    "com_name" = "\U5e7f\U5dde\U5929\U8109";
                    info =             (
                                        {
                                            "section_name" = "\U6280\U672f\U90e8";
                                            "user_name" = "\U8463\U5bcc\U7f8e";
                                            "user_phone" = 18378164697;
                                        },
                                        {
                                            "section_name" = "\U6280\U672f\U90e8";
                                            "user_name" = "\U5362\U6d3b\U6e5b";
                                            "user_phone" = 13557149890;
                                        }
                                        );
                    section =             (
                                           {
                                               "section_name" = "\U6280\U672f\U90e8";
                                           }
                                           );
                }
                );
    message = "\U67e5\U8be2\U6210\U529f";
    userinfo =     {
        "id_card" = 450802199311153620;
        "member_name" = "\U8463\U5bcc\U7f8e\U7f8e";
        "phone_name" =         (
                                {
                                    phone = 18378164697;
                                }
                                );
    };
}
 */

+ (UserDataModel *)loginModelArrayJsonDic:(NSDictionary *)dic {
    UserDataModel *model = [UserDataModel new];
    NSDictionary *userinfo = dic[@"userinfo"];
    [model setValuesForKeysWithDictionary:userinfo];
    
    NSMutableArray *dataModelArr = [NSMutableArray array];
    NSArray *data = dic[@"data"];
    for (NSDictionary *dataDic in data) {
        CompanyModel *cModel = [CompanyModel modelWithDic:dataDic];
        if (cModel) {
            [dataModelArr addObject:cModel];
        }
    }
    
    model.data = dataModelArr;
    
    return model;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

#pragma mark -- 增取删

+ (BOOL)saveModel:(UserDataModel *)model {
    if (!model) {
        return NO;
    }
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
    if (!data) {
        return NO;
    }
    
    [UD setObject:data forKey:UD_UserData];
    return [UD synchronize];
}

+ (UserDataModel *)getLocalModel {
    NSData *data = [UD objectForKey:UD_UserData];
    if (!data) {
        return nil;
    }
    
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

+ (BOOL)clearLocalModel {
    [UD removeObjectForKey:UD_UserData];
    return [UD synchronize];
}



@end

















